package com.ruoyi.business.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.business.domain.Indicator;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessStatus;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.AddressUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.service.ISysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Year;
import java.util.Calendar;
import java.util.Date;

@Service
public class BaseServiceImpl {

    @Autowired
    private RedisCache redisCache;
    public void setCacheInfo(String runInfoTable,String runInfoSuccess,String runInfofail,String runInfoNum){

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("runInfoTable",runInfoTable);
        jsonObject.put("runInfoSuccess",runInfoSuccess);
        jsonObject.put("runInfofail",runInfofail);
        jsonObject.put("runInfoNum",runInfoNum);

        redisCache.deleteObject("run-info-msg");
        redisCache.setCacheObject("run-info-msg", jsonObject);

        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // *========数据库日志=========*//
        SysOperLog operLog = new SysOperLog();
        operLog.setTitle("数据管理");
        operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
        // 请求的地址
        String ip = IpUtils.getIpAddr();
        operLog.setOperIp(ip);
        operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255));
        if (loginUser != null)
        {
            operLog.setOperName(loginUser.getUsername());
            SysUser currentUser = loginUser.getUser();
            if (StringUtils.isNotNull(currentUser) && StringUtils.isNotNull(currentUser.getDept()))
            {
                operLog.setDeptName(currentUser.getDept().getDeptName());
            }
        }
        // 设置方法名称
        operLog.setMethod("edit");
        // 设置请求方式
        operLog.setRequestMethod(ServletUtils.getRequest().getMethod());

        // 处理设置注解上的参数
        String templateOpt = String.format("数据管理，%s  上传完成，成功 %s 条，%s 条异常（行号 %s）。",
                runInfoTable,runInfoSuccess,runInfofail,runInfoNum
        );
        operLog.setJsonResult(templateOpt);
        // 设置消耗时间
        operLog.setCostTime(0L);

        operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp()));
        SpringUtils.getBean(ISysOperLogService.class).insertOperlog(operLog);

    }


    public boolean isAwardYearValid(Date award) {
        // 获取当前年份
        int currentYear = Year.now().getValue();

        // 获取奖项年份
        // 使用Calendar获取奖项年份
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(award);
        int awardYear = calendar.get(Calendar.YEAR);

        // 判断年份是否在2000年到当前年之间
        return awardYear >= 2000 && awardYear <= currentYear;
    }
}
